package com.example.service.serviceImpl;

import com.example.mapper.LoginMapper;
import com.example.mapper.UpdatePasswordMapper;
import com.example.pojo.UpdatePasswordInfo;
import com.example.pojo.User;
import com.example.service.UpdatePasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UpdatePasswordServiceImpl implements UpdatePasswordService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private UpdatePasswordMapper updatePasswordMapper;

    /**
     * 修改用户密码
     */
    @Override
    public void updatePassword(UpdatePasswordInfo updatePasswordInfo) throws Exception {
        User user = loginMapper.findByUsernameAndPassword(updatePasswordInfo.getUsername(), updatePasswordInfo.getOldPassword());
        if (user == null) {
            throw new Exception("旧密码填写错误");
        }

        user.setPassword(updatePasswordInfo.getNewPassword());
        updatePasswordMapper.updatePassword(user);
    }
}
